package com.pic.mgt.syushin.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.pic.mgt.action.AbstractMissUnitInspectActionImpl;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.monitor.parser.MessageTransactionFileParser;

// (送多方修改)
public class SyushinMissMessageUnitInspectActionImpl extends AbstractMissUnitInspectActionImpl {
	
	private SyushinMessageRecoverAction syushinMessageRecoverAction;
	
	private long syushinInspectThreshold;
	private String msgFilenameSuffix;
	
	private SyushinBusinessApMgtAction syushinBusinessApMgtAction;
	
	public SyushinMissMessageUnitInspectActionImpl() {
		super();
	}

	@Override
	public synchronized boolean inspectMissUnitAndRecover() {
		// ********************** Log **********************
		IgxLog.messageLog.info(this, "Starting miss message inspection process.");
		// ********************** Log **********************
		
		// 取出所有集信的 AP 目錄
		List<File> apFolderList = this.syushinBusinessApMgtAction.getAllApFolders();
		
		// 取得現在時間
		Date nowDate = new Date();
		
		SuffixFilenameFilter suffixFilenameFilter = new SuffixFilenameFilter(this.msgFilenameSuffix);
		
		for (File apFolder : apFolderList) {
			// 檢測所有 syushin AP 目錄下所有訊息確認檔是否有存在超過指定的時間
			File[] missSyushinMessageFileArray =
				super.checkFileMissUnderFolder(apFolder,
											   nowDate,
											   this.syushinInspectThreshold,
											   suffixFilenameFilter);
			if (null != missSyushinMessageFileArray) {
				for (File missMessageFile : missSyushinMessageFileArray) {
					// 如果開啟忽略 0KB 檔案的功能,則要做檢查 (一般 Message 不會開啟此功能)
					if (super.isIgnoreEmptyMsgConfirmationFile) {
						if (missMessageFile.length() <= 0) {
							continue; // 表0KB的確認檔,則忽略之
						}
					}
					
					// ********************** Log **********************
					IgxLog.messageLog.info(this,
									"Miss Message Inspection: detected a miss message '{}'.",
									missMessageFile.getAbsolutePath());
					// ********************** Log **********************
					
					// 進行訊息恢復
					File transactionFile = MessageTransactionFileParser.getTransactionFile(missMessageFile);
					this.syushinMessageRecoverAction.recoverMessage(transactionFile.getName(), apFolder.getName());
				}
			}
		}
		
		// ********************** Log **********************
		IgxLog.messageLog.info(this, "Miss message inspection process completed.");
		// ********************** Log **********************
		
		return true;
	}
	
	// --- getter and setter ---


	public void setSyushinMessageRecoverAction(
			SyushinMessageRecoverAction syushinMessageRecoverAction) {
		this.syushinMessageRecoverAction = syushinMessageRecoverAction;
	}

	public void setSyushinInspectThreshold(long syushinInspectThreshold) {
		this.syushinInspectThreshold = syushinInspectThreshold;
	}

	public void setMsgFilenameSuffix(String msgFilenameSuffix) {
		this.msgFilenameSuffix = msgFilenameSuffix;
	}

	public void setSyushinBusinessApMgtAction(
			SyushinBusinessApMgtAction syushinBusinessApMgtAction) {
		this.syushinBusinessApMgtAction = syushinBusinessApMgtAction;
	}
}
